class Solution
{
public:
    int longestSquareStreak(vector<int> &nums)
    {
        set<long long> s(nums.begin(), nums.end());
        int longestLen = -1;
        while (!s.empty())
        {
            int len = 1;
            long long curNum = *s.begin();
            s.erase(curNum);
            while (s.count(curNum * curNum))
            {
                curNum *= curNum;
                s.erase(curNum);
                ++len;
            }
            if (len >= 2)
            {
                longestLen = max(len, longestLen);
            }
        }
        return longestLen;
    }
};